/*封装了jquery的$.ajax()方法，主要是对session超时时，判断服务器返回状态，进行相应的跳转*/
(function($){

	if (window.location.href.indexOf("toLogin") != -1) {
		if($(".ems_main_content",parent.document).length > 0) {
			window.parent.location.href = "/starCms/toLogin";
		}
	}

	$.emsAjax = function(options) {
		options = $.extend({
			cache:false,
			async:false,
			type:"POST"
		},options);
		
		$.ajax({
			cache:options.cache,
			async:options.async,
			url:options.url,
			data:options.data,
			type:options.type,
			timeout:options.timeout,
			success:function(response){
				if (response.status == "login") {
					window.parent.location.href = "/starCms/toLogin";
				}else{
					options.success(response);
				}
			},
			error:function(response) {
				console.log(response);
				if (options.error) {
					options.error(response);
				}
			},
			beforeSend: options.beforeSend
		});
		
	};
	
	var basicLoadingTip =
		"<div class='cf-loading'>" +
			"<div class='modal-backdrop in'></div>" +
			"<div class='loading'>努力加载中,请稍后" +
				"<img src='/images/loading.gif' style='margin-left: 60px;' />" +
			"</div>" +
		"</div>";
	$.beforeLoadingHandler = function () {
		$("body").append(basicLoadingTip);
		$(".cf-loading").find(".modal-backdrop").css("z-index","1040");
	};
	
	$.afterLoadingHandler = function () {
		$(".cf-loading").remove();
	};
	
	$.emsPost = function(url, data, callback){
		var options = {
			async: true,		//异步调用
			url: url,			//URL
			data: data,			//后台传参
			success: callback	//回调函数
		};
		$.emsAjax(options);
	};
})(jQuery);

$.toast.prototype.defaults.duration = 1000;

// 手机号码验证
jQuery.validator.addMethod("isMobile", function(value, element) {
    var length = value.length;
    var mobile = /^(13[0-9]{9})|(18[0-9]{9})|(14[0-9]{9})|(17[0-9]{9})|(15[0-9]{9})$/;
    return this.optional(element) || (length == 11 && mobile.test(value));
}, "请正确填写您的手机号码");

